import gql from "graphql-tag"
import useQuery from "./useQuery"
import { UserEntity, UserFields } from "@/types/UserEntity"
import { useEffect, useMemo } from "react"
import Taro from "@tarojs/taro"

const ViewerQuery = gql`
    query Viewer{
        viewer{
            ${UserFields}
        }
    }
`
const useViewer = (jump = true) => {
    const { data, error, refetch, loading } = useQuery<{ viewer: UserEntity }>(ViewerQuery)
    const user = useMemo(() => data?.viewer, [data]);
    useEffect(() => {
        if (jump && !loading) {
            if (!user) {
                Taro.navigateTo({ url: '/pages/Login/index' });
            }
        }
    }, [data, loading])
    return { data: user, error, refetch, loading }
}
export default useViewer